#拼多多备注信息

from common.config import *
import requests,json,time
from common.Helper import Helper



class PddNotes( object ):


    def __init__(self):

        pass

    def requestNotes(self,task=None):

        resp = {'type': 103, 'msg': '备注任务','content':{}}
        # 设置cookie
        header['cookie'] = task['cookie']

        data = {"orderSn":task['order_sn'],"remark":task['content']}

        r = requests.post(url=domain_url['notes_url'],headers=header,data=json.dumps( data ))

        if r.status_code == 200 and not r.json()['success']:

            req = r.json()

            try:
                # 传参错误
                error = dict( errorCode= req['errorCode'],errorMsg=req['errorMsg'])

            except:
                # cookie已过期
                error = dict( errorCode= req['error_code'],errorMsg=req['error_msg'])

            resp['content'] = task
            resp['content']['errcode'] = error['errorCode']
            resp['content']['errmsg'] = error['errorMsg']
            resp['content']['data'] = {}

            content = Helper.changeBase64(json.dumps(resp['content']))
            resp['content'] = content.decode('ascii')

            return resp


        #成功状态
        else:

            resp['content']['content'] = task

            resp['content']['data'] = {

                "owner_id":task['owner_id'],
                "order_sn":task["order_sn"],
                "created_time":int(time.time())

            }

            resp['content']['errcode'] = 0
            resp['content']['errmsg'] = "任务成功"

            # print( resp )
            content = Helper.changeBase64(json.dumps(resp['content']))

            resp['content'] = content.decode('ascii')

            print('%s执行备注成功' % (data['orderSn']))

            return resp


    def parseIndex(self):

        pass



if __name__ == '__main__':

    n = PddNotes()

    task = {
          "type": 203,
           "content":  {
            "task_order_id":  7,
             "owner_id":  186716817,
             "task_id":  2,
             "order_sn":  "190628-221974365071638",
             "content":  "Qi",
             "cookie":  "_nano_fp=Xpdjl0UyXpEqn5Tjn9_hcqX3_GdIDNGobRfgDlMO;_nano_fp=Xpdjl0UyXpEqn5Tjn9_hcqX3_GdIDNGobRfgDlMO;PASS_ID=1-A2WKIav5GBsz0Jp8QV6+GP5ff+Sw2kjokhgReoom02NzvMmPD/7ZQL4zxnQq4HDoUpHKB7I9J/VtNHDlNsp5YQ_186716817_29159208"
          },
           "msg":  "备注信息任务"
        }

    task = task['content']

    print( n.requestNotes( task ) )